package com.example.mybatisdemo.util;



/**
 * 公共响应结果成功失败的静态方法调用
 * @author liuyingjun
 * @datetime 2018-06-22 17:18
 **/
public class BaseResultUtil {

    /**
     * return success
     * @return
     */
    public static <T> CommonResult<T> returnSuccess() {
        CommonResult<T> result = new CommonResult();
        result.setCode(CommonResultCode.SUCCESS.getCode());
        result.setOk();
        result.setMessage(CommonResultCode.SUCCESS.getMessage());
        return result;
    }

    /**
     * return success
     * @return
     */
    public static <T> CommonResult<T> returnSuccess(String message) {
        CommonResult<T> result = new CommonResult();
        result.setCode(CommonResultCode.SUCCESS.getCode());
        result.setOk();
        result.setMessage(message);
        return result;
    }

    /**
     * return success
     * @return
     */
    public static <T> CommonResult<T> returnSuccessData(T data) {
        CommonResult<T> result = new CommonResult();
        result.setCode(CommonResultCode.SUCCESS.getCode());
        result.setOk();
        result.setData(data);
        result.setMessage(CommonResultCode.SUCCESS.getMessage());
        return result;
    }




    /**
     * return success
     *
     * @param data
     * @return
     */
    public static <T> CommonResult<T> returnSuccess(T data) {
        CommonResult<T> result = new CommonResult();
        result.setCode(CommonResultCode.SUCCESS.getCode());
        result.setOk();
        result.setData(data);
        result.setMessage(CommonResultCode.SUCCESS.getMessage());
        return result;
    }

    /**
     * return error
     * @param msg  error message
     * @return
     */
    public static CommonResult returnError(String msg) {
        CommonResult result = new CommonResult();
        result.setCode(CommonResultCode.ERROR.getCode());
        //result.setData("");
        result.setMessage(msg);
        return result;
    }

    /**
     */
    public static CommonResult returnFail() {
        CommonResult result = new CommonResult();
        result.setCode(CommonResultCode.FAIL.getCode());
        result.setMessage(CommonResultCode.FAIL.getMessage());
        return result;
    }

    /**
     * return error
     * @param msg  error message
     * @return
     */
    public static CommonResult returnFail(String msg) {
        CommonResult result = new CommonResult();
        result.setCode(CommonResultCode.FAIL.getCode());
        result.setMessage(msg);
        return result;
    }
 
    /**
     * return error
     *
     * @param code error code
     * @param msg  error message
     * @return
     */
    public static CommonResult returnError(int code, String msg) {
        CommonResult result = new CommonResult();
        result.setCode(code);
        result.setMessage(msg);
        return result;
 
    }


    /**
     * return error
     *
     * @param code error code
     * @param msg  error message
     * @return
     */
    public static CommonResult returnError(int code, String msg, String description) {
        CommonResult result = new CommonResult();
        result.setCode(code);
        result.setMessage(msg);
        result.setDescription(description);
        return result;

    }
 
    /**
     * use enum
     *
     * @param status
     * @return
     */
    public static CommonResult returnError(IResultCode status) {
        return returnError(status.getCode(), status.getMessage());
    }

    /**
     * use enum
     *
     * @param status
     * @return
     */
    public static <T> CommonResult returnErrorData(IResultCode status,T data) {
        CommonResult result = new CommonResult();
        result.setCode(status.getCode());
        result.setMessage(status.getMessage());
        result.setData(data);
        return result;
    }

    /**
     * use enum
     *
     * @param status
     * @return
     */
    public static CommonResult returnError(IResultCode status, String description) {
        return returnError(status.getCode(), status.getMessage(), description);
    }
}
